﻿/// <reference path="../knockout-2.2.0.js" />
/// <reference path="rover.js" />
/// <reference path="marsrovers.js" />


MARSROVERS.namespace("MARSROVERS.PLATEAVIEW");

/**
 * 
 * @module PLATEAUVIEW
 * Controls the behavior of the plateau's index view. 
 */
MARSROVERS.PLATEAVIEW = (function ($,Rover) {

    /**
    * Constructor, creates new viewmodel object
    * @method ViewModel
    * @return {Object} new viewmodel object 
    */
    function ViewModel() {
        var self = this;
        self.RoverView = ko.observable(false);
        self.PlateauView = ko.observable(true);
        self.Rovers = ko.observableArray([]);
        self.CurrentRover = ko.observable();
        self.Movements = ko.observableArray([]);
        self.RoverInstructionsPartialView = ko.observable(false);
        self.RoverConfigPartialView = ko.observable(false);
        self.RoverMenuPartialView = ko.observable(false);
        
    }

   
    ViewModel.prototype.btnContinueClick = function() {
        this.RoverView(true);
        this.RoverMenuPartialView(true);
        this.PlateauView(false);
    };

    ViewModel.prototype.btnGoToInstructionsClick = function () {
        //Get X,Y  and orientation parameters for the new rover
        var xPosition = Number($("#roverXCoordinate").val()),
            yPosition = Number($("#roverYCoordinate").val()),
            orientation = $("#orientation").val(),
            plateau = {},
            rover = new Rover(xPosition, yPosition, orientation);

        plateau.X = Number($("#plateauXCordinate").val());
        plateau.Y = Number($("#plateauYCordinate").val());

        //Clean fields
        $("#roverXCoordinate").val("");
        $("#roverYCoordinate").val("");

        rover.setPlateau(plateau);
        this.CurrentRover(rover);
        this.RoverConfigPartialView(false);
        this.RoverInstructionsPartialView(true);
    };

    ViewModel.prototype.btnNewRoverClick = function () {
        this.RoverMenuPartialView(false);
        this.RoverConfigPartialView(true);
    };

    ViewModel.prototype.btnStopMovingClick = function () {
        this.RoverInstructionsPartialView(false);
        var rover = this.CurrentRover();
        this.Rovers.push(rover);
        this.RoverMenuPartialView(true);
        this.Movements([]);
        $("#instructions").val("");
    };

    ViewModel.prototype.btnBackToPlateauConfigClick = function () {
        this.RoverView(false);
        this.RoverMenuPartialView(false);
        this.PlateauView(true);
        this.Rovers([]);
    };
    
    ViewModel.prototype.btnBackToMenu = function () {
     
        //Clean fields
        $("#roverXCoordinate").val("");
        $("#roverYCoordinate").val("");

        this.RoverMenuPartialView(true);
        this.RoverConfigPartialView(false);
        this.RoverInstructionsPartialView(false);
    };

    ViewModel.prototype.txtBoxInstructionKeyPress = function (data,event) {
        var instruction = event.char || String.fromCharCode(event.charCode);
        this.CurrentRover().move(instruction);
        var movement = this.CurrentRover().getLastMovement();
        this.Movements.push(movement);
        return true;
    };

    return {
        /**
        * initializes view index bindings. All bindings are controlled by knockoutjs
        * @method initialize
        */
        initialize: function() {
            ko.applyBindings(new ViewModel());
        }
    };

}(jQuery,MARSROVERS.ROVER))